package com.rykj.qxj.server.services.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.rykj.qxj.server.common.exception.GlobalQueriesPerSecondPutExceptiuon;
import com.rykj.qxj.server.common.exception.SinglQueriesPerSecondPutExceptiuon;
import com.rykj.qxj.server.dao.SystemParameterMapper;
import com.rykj.qxj.server.model.entity.ApiVersionManage;
import com.rykj.qxj.server.model.entity.SystemParameter;
import com.rykj.qxj.server.services.ISystemParameterService;
import com.techindata.cacheredis.CachePut;

@Service
public class SystemParameterServiceImpl implements ISystemParameterService  {
	
	@Autowired
	private SystemParameterMapper systemParameterMapper;
    //单个接口的QPS
	private static final String SINGL_QUERIES_PER_SECOND="singlQueriesPerSecond";
	//全局所有接口的QPS
	private static final String GLOBAL_QUERIES_PER_SECOND="globalQueriesPerSecond";
	/*@Transactional(rollbackFor=Exception.class)
	public void update(SystemParameter systemParameter) throws SinglQueriesPerSecondPutExceptiuon, GlobalQueriesPerSecondPutExceptiuon {
		SystemParameter oldSystemParameter = systemParameterMapper.selectByPrimaryKey(systemParameter.getId());
		oldSystemParameter.setValue(systemParameter.getValue());
		oldSystemParameter.setUpdateTime(systemParameter.getUpdateTime());
		int update = systemParameterMapper.updateByPrimaryKeySelective(oldSystemParameter);
		if(oldSystemParameter!=null&&oldSystemParameter.getParamKey().equals(SINGL_QUERIES_PER_SECOND)) {
			SystemParameter selectSystemParameterByParamKey = systemParameterMapper.selectSystemParameterByParamKey(SINGL_QUERIES_PER_SECOND);
			boolean putCache = CachePut.putCache(SINGL_QUERIES_PER_SECOND, selectSystemParameterByParamKey.getValue().toString());
			if(!putCache) {
				throw new SinglQueriesPerSecondPutExceptiuon("单个接口的QPS更新缓存失败,请检查原因。");
			}
		}
		if(oldSystemParameter!=null&&oldSystemParameter.getParamKey().equals(GLOBAL_QUERIES_PER_SECOND)) {
			SystemParameter selectSystemParameterByParamKey = systemParameterMapper.selectSystemParameterByParamKey(GLOBAL_QUERIES_PER_SECOND);
			boolean putCache = CachePut.putCache(GLOBAL_QUERIES_PER_SECOND, selectSystemParameterByParamKey.getValue().toString());
			if(!putCache) {
				throw new GlobalQueriesPerSecondPutExceptiuon("全局所有接口的QPS更新缓存失败,请检查原因。");
			}
		}
	}*/
	public PageInfo selectSystemParameterBySearchParams(Integer pageNum, Integer pageSize, String key) {
		PageHelper.startPage(pageNum, pageSize);
		Map<String,Object> paramMap=new HashMap<String,Object>();
		paramMap.put("paramkey", key);
		List<ApiVersionManage> apiVersionManageList = systemParameterMapper.selectSystemParameterBySearchParams(paramMap);
		PageInfo page=new PageInfo<>(apiVersionManageList);
		return page;
	}
	public SystemParameter selectSystemParameterByParamKey(String paramKey) {
		return systemParameterMapper.selectSystemParameterByParamKey(paramKey);
		
	}
	@Override
	public void save(SystemParameter systemParameter) {
		systemParameterMapper.insert(systemParameter);
		
	}
	@Override
	public void update(SystemParameter systemParameter) {
		systemParameterMapper.updateByPrimaryKeySelective(systemParameter);
		
	}
	@Override
	public SystemParameter selectSystemParameterById(Integer id) {
		return systemParameterMapper.selectByPrimaryKey(id);
	}

}
